package com.gzgy.ustore.api.inteceptors;

import com.gzgy.ustore.api.common.exception.BaseAPIException;
import com.gzgy.ustore.api.common.util.EncryptionUtils;
import com.gzgy.ustore.api.common.util.I18nUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by jancekyang on 2017/7/14.
 */
public class InteceptorApp implements HandlerInterceptor {

    private Log logger = LogFactory.getLog("REQUEST_APP_PARAMETER_LOG");

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {

        //打印 log
        InteceptorUtil.printParamLog(logger,httpServletRequest);
        //获取传递的加密值
        String access_data = httpServletRequest.getParameter("access_data");
        Assert.hasText(access_data, "access_data" + I18nUtil.getMessage(-2,null,"zh_CN"));
        //解密
        String decryptAesData =  EncryptionUtils.decryptAES(access_data,EncryptionUtils.KEY,EncryptionUtils.IV);
        if(StringUtils.isEmpty(decryptAesData)){
            throw new BaseAPIException("加密参数有误");
        }
        logger.info("解密参数 :" + decryptAesData) ;
        httpServletRequest.setAttribute("dp",decryptAesData);

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
